package com.guo.rabbitmq.api.exchange.direct;

import com.guo.rabbitmq.constant.RabbitMqConstant;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author guo
 * @date 2021/11/17 18:45 星期三
 **/
public class Producer4DirectExchange {
    public static void main(String[] args) throws Exception{

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(RabbitMqConstant.HOST);
        connectionFactory.setPort(RabbitMqConstant.PORT);
        connectionFactory.setVirtualHost(RabbitMqConstant.VIRTUAL_HOST);

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();
        channel.confirmSelect();

        String routingKey = "test.direct.exchange";
        String exchange = "test.directExchange";

        Map<String, Object> headers = new HashMap<>(2);
        headers.put("latitude",  51.5252949);
        headers.put("longitude", -0.0905493);

        for (int i = 0; i < 500; i++) {

            byte[] messageBodyBytes = (LocalDateTime.now() + "  ==>  Hello, world! msg："+i).getBytes();
            Thread.sleep(100);
            channel.basicPublish(exchange,
                    routingKey,
                    new AMQP.BasicProperties.Builder()
                            .headers(headers)
                            .deliveryMode(2)
                            .contentEncoding("UTF-8")
//                            .expiration("10000")
                            .build(),
                    messageBodyBytes);

            channel.addConfirmListener(new ConfirmListener() {
                @Override
                public void handleAck(long deliveryTag, boolean multiple) throws IOException {

                }

                @Override
                public void handleNack(long deliveryTag, boolean multiple) throws IOException {

                }
            });

        }


        channel.close();
        connection.close();



    }
}
